package com.qf.web.controller;/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/20
 */

import com.qf.domain.Address;
import com.qf.domain.Cart;
import com.qf.domain.Order;
import com.qf.domain.User;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * projectName:Project_2202
 * author:
 * time:2022/4/20 13:58
 * description:
 */
@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet {
    CartService cartService = new CartServiceImpl();
    AddressService addressService = new AddressServiceImpl();
    OrderService orderservice = new OrderServiceImpl();
    public String getOrderView(HttpServletRequest request, HttpServletResponse response)throws IOException {
        //获取购物车信息
        User user = (User)request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";

        }
        List<Cart> carts = cartService.findCardByUid(user.getId());
        request.setAttribute("carts", carts);
        //查询该用户的地址列表信息
        List<Address> addList = addressService.findAddListByUid(user.getId());

        request.setAttribute("addList", addList);

        return "forward:/order.jsp";
    }

    public String addOrder(HttpServletRequest request, HttpServletResponse response)throws IOException {

        User user =(User)request.getSession().getAttribute("user");

        String aid = request.getParameter("aid");

        Order order = orderservice.addOrder(user.getId(),aid);

        return "forward:/orderSuccess.jsp";
    }
}